﻿using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace JupyterLauncher
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void 启动笔记本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Run();
        }

        private void 关闭笔记本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close("jupyter");
            Close("jupyter-notebook");
            Environment.Exit(0);
        }

        public string url = "http://localhost:8889/tree";

        private void Run()
        {
            try
            {
                Process[] processes = Process.GetProcessesByName("jupyter-notebook");
                Process[] processesOfJupyter = Process.GetProcessesByName("jupyter");
                if (processes.Length > 0 && processesOfJupyter.Length > 0)
                {
                    OpenUrlInBrowser(url);
                }
                else
                {
                    Process process = new Process();
                    process.StartInfo.FileName = "cmd.exe";
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.CreateNoWindow = true;
                    process.StartInfo.RedirectStandardInput = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;

                    // 启动程序
                    process.Start();
                    process.StandardInput.WriteLine("jupyter notebook");
                    process.StandardInput.Flush();
                    process.StandardInput.Close(); // 关闭输入流
                }
            }
            catch
            {

            }
        }

        public static void Close(string name)
        {
            Process[] processes = Process.GetProcessesByName(name);
            foreach (Process process in processes)
            {
                // 尝试关闭进程
                try
                {
                    process.CloseMainWindow(); // 尝试温和地关闭主窗口
                    if (!process.HasExited)
                    {
                        process.Kill(); // 如果进程没有退出，强制结束进程
                    }
                }
                catch (Exception ex)
                {
                    // 处理可能出现的异常，例如进程已经退出或者没有权限关闭进程
                    Console.WriteLine($"关闭进程时出错: {ex.Message}");
                }
            }
        }

        public static void OpenUrlInBrowser(string url)
        {
            try
            {
                ProcessStartInfo psi = new ProcessStartInfo
                {
                    FileName = url,
                    UseShellExecute = true
                };
                Process.Start(psi);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"打开网址时出错: {ex.Message}");
            }
        }
    }
}
